<?php

namespace App\Repositories;

use App\Models\Bonus;
use App\Models\BonusCategory;
use App\Models\Category;
use InfyOm\Generator\Common\BaseRepository;

/**
 * Class BonusRepository
 * @package App\Repositories
 * @version September 16, 2017, 3:58 am UTC
 *
 * @method Bonus findWithoutFail($id, $columns = ['*'])
 * @method Bonus find($id, $columns = ['*'])
 * @method Bonus first($columns = ['*'])
*/
class BonusRepository extends BaseRepository
{
    /**
     * @var array
     */
    protected $fieldSearchable = [
        'name',
        'user_id',
        'distribute_id',
        'money',
        'type',
        'min_amount',
        'max_amount',
        'send_start_date',
        'send_end_date',
        'use_start_date',
        'use_end_date',
        'use_date',
        'use_status'
    ];

    /**
     * Configure the Model
     **/
    public function model()
    {
        return Bonus::class;
    }

    public function dispatchRandom($random=[],$id, $shop_id=null)
    {
        $count = count($random);
        $c = random_int(1,$count);
        $this->dispatch($random[$c-1], $id, $shop_id);
    }

    public function dispatch($name,$id, $shop_id=null)
    {
        $bonus = Bonus::where([
            ['shop_id',$shop_id],
            ['name',$name],
            ['user_id',0],
        ])->first();
        if($bonus){
            $bonus->user_id = $id;
            $bonus->use_end_date = date('Y-m-d',time()+3600*24*30);
            $bonus->save();
        }else{
            $old = Bonus::where([
                ['name',$name],
                ['shop_id',$shop_id],
            ])->first();
            if($old){
                $new = Bonus::create([
                    'name'=>$old->name,
                    'shop_id'=>$shop_id,
                    'sn'=>date('YmdHis').mt_rand(1000,9999),
                    'user_id'=>$id,
                    'money'=>$old->money,
                    'type'=>$old->type,
                    'min_amount'=>$old->min_amount,
                    'max_amount'=>$old->max_amount,
                    'send_start_date'=>$old->send_start_date,
                    'send_end_date'=>$old->send_end_date,
                    'use_start_date'=>$old->use_start_date,
                    'use_end_date'=>date('Y-m-d',time()+3600*24*30),
                ]);
            }
        }
    }

    public function dispatchByTypeLimit(BonusCategory $bondCate,$user_id)
    {

        $count = Bonus::where([
            ['bonus_category_id',$bondCate->id],
            ['user_id',$user_id],
        ])->count();
        if($count<$bondCate->limit_number){
            $bonus = Bonus::create([
                'name' => $bondCate->name,
                'shop_id' => $bondCate->shop_id,
                'bonus_category_id' => $bondCate->id,
                'sn' => date('YmdHis') . mt_rand(1000, 9999),
                'user_id' => $user_id,
                'money' => $bondCate->money,
                'type' => $bondCate->type,
                'min_amount' => $bondCate->min_amount,
                'max_amount' => $bondCate->max_amount,
                'send_start_date' => $bondCate->send_start_date,
                'send_end_date' => $bondCate->send_end_date,
                'use_start_date' => $bondCate->use_start_date,
                'use_end_date' => $bondCate->use_start_date,
            ]);
            return $bonus;
        }else{
            return false;
        }
    }
}
